package cn.agiledata.bank.information.dao;

import java.sql.Clob;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.lob.SerializableClob;

import cn.agiledata.bank.hibernateBean.Information;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

public class InformationDaoImpl extends IPBHibernate3BaseMgr implements IInformationDao {
    String date = " and ? >= t.publishStartdate  and ? <= t.publishEnddate";
	public List getInformationByKind(String kind) {
		String hql = "from Information t where t.kind = "+kind+ date+"  and order by t.publishStartdate desc";
		//Query query = getHibernateTemplate().getSessionFactory().openSession().createQuery(hql).setParameter("now",new Date());
		Date now = new Date();
		List list = this.getHibernateTemplate().find(hql,new Date[]{now,now});
		return list;
	}
    
	public Information getInformationById(String id){
		String hql = "from Information t where t.id = "+id;
		List list = getHibernateTemplate().find(hql);
		
		Information information = (Information) list.get(0);
		if (null != information.getIcontent()
				&& !information.getIcontent().equals("")) {
			SerializableClob sc = (SerializableClob) information.getIcontent();

			Clob wrapclob = sc.getWrappedClob();
			if (wrapclob instanceof Clob) {
				Clob clob = (Clob) wrapclob;
				int length;
				String content = "";
				try {
					length = (int) clob.length();
					content = clob.getSubString(1, length);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				information.setContent(content);
			}
		}
		return information;
	}

	public List getInformationByKindDate(String kind, Date now) {
		String hql = "from Information t where t.kind = "+kind + date+"  and t.status='Y' order by t.publishStartdate desc" ;
		List resultList = this.getHibernateTemplate().find(hql,new Date[]{now,now});
		return resultList;
	}
	public List getInformationByKindOccDate(String kind){
		String hql = "from Information t where t.kind = "+kind + date+" and t.status='Y' order by t.occurDate desc";
		//Query query = getHibernateTemplate().getSessionFactory().openSession().createQuery(hql).setParameter("now", new Date());
		Date now = new Date();
		List list = this.getHibernateTemplate().find(hql,new Date[]{now,now});
		return list;
	}

	public List getInformationByType(String type) {
		String hql="from Information t where t.kind = '1' and  t.status='Y' and t.type = "+ type+ date+" order by t.publishStartdate desc";
		Date now = new Date();
		List list = this.getHibernateTemplate().find(hql,new Date[]{now,now});
		return list;
	}

	public List getInformationPick(String pick) {
		String hql="from Information t where t.kind = '1' and  t.status='Y' and t.pick = '"+ pick+"'  "+date+" order by t.publishStartdate desc";
		Date now = new Date();
		return this.getHibernateTemplate().find(hql,new Date[]{now,now});
		
	}

	public List getInformatinByTwoType(String type1, String type2) {
		String hql="from Information t where t.kind = '1' and  t.status='Y' and ( t.type = "+ type1 +"or t.type="+type2+")"+ date+" order by t.publishStartdate desc";
		Date now = new Date();
		List list = this.getHibernateTemplate().find(hql,new Date[]{now,now});
		return list;
	}
}